﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpGen.Controls;
using SharpGen.Templates;

namespace SharpGen.Forms
{
    public partial class FindReplaceForm : Form
    {
        public StringReplacementCollection StringReplacementList { get; private set; }

        //public FindReplaceForm()
        //    : this(null)
        //{
        //}

        public FindReplaceForm(StringReplacementCollection stringReplacementList)
        {
            InitializeComponent();
            StringReplacementList = new StringReplacementCollection();
            if (stringReplacementList != null)
            {
                foreach (StringReplacement sr in stringReplacementList)
                {
                    AddStringReplacementControl(sr);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddStringReplacementControl(null);
        }

        public void AddStringReplacementControl(StringReplacement stringReplacement)
        {
            FindReplace findReplace = new FindReplace();
            findReplace.Close += new EventHandler(findReplace_Close);
            findReplace.Change += new EventHandler(findReplace_Change);
            //findReplace.Dock = DockStyle.Fill;
            findReplace.Options = stringReplacement;
            StringReplacementList.Add(findReplace.Options);
            findReplaceContainer.Controls.Add(findReplace);
        }

        void findReplace_Change(object sender, EventArgs e)
        {
            FindReplace findReplace = sender as FindReplace;
            int index = findReplaceContainer.Controls.IndexOf(findReplace);
            if (index >= 0)
            {
                StringReplacementList[index] = findReplace.Options;
            }
        }

        void findReplace_Close(object sender, EventArgs e)
        {
            FindReplace findReplace = sender as FindReplace;
            int index = findReplaceContainer.Controls.IndexOf(findReplace);
            if (index >= 0)
            {
                StringReplacementList.RemoveAt(index);
                findReplaceContainer.Controls.Remove(findReplace);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Hide();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (this.DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                StringReplacementList.Clear();
                findReplaceContainer.Controls.Clear();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            StringReplacementList.Clear();
            findReplaceContainer.Controls.Clear();
        }
    }
}
